//void, Obj This

bool bRespawn;
str items, item; 
Building this;
ObjList olBarbarian;
int count, idx, minlevel, timetorest;
int brespawn, timeup, i, randb;
str class_unit;
Item item_obj;
Hero hero;
Unit uBarbarian;
point pos, p;
rect rc;

Sleep(5020+rand(GetConst("RandTimeStart"))+GetConst("AddTimeStart"));

this = This.AsBuilding;
minlevel = .minlevel;
EnvWriteInt(this, "minlevel", minlevel);
bRespawn = true;
timetorest = 0;
brespawn = 0;

while(1)
{
	if(bRespawn)
	{
		// first count 'em
		count = 0;
		items = .itemtypes;
		while(items != "")
		{
			item = ParseStr(items, items);
			count += 1;
		}
		idx = rand(count);

		items = .itemtypes;
		while(idx > 0)
		{
			item = ParseStr(items, items);
			idx -= 1;
		}
		.AddItem(item);
		item_obj = .FindItem(item);
		bRespawn = false;
	}
	if(.settlement.UnitsCount > 0)
	{
		hero = .settlement.Units[0].AsHero();
		if (hero.IsAlive)
		{
			hero.Deselect();
			Sleep(10000);
			if (hero.IsAlive)
			if (hero.InHolder)
			{
				bool bCanGetItem = false;
				//if (10 * hero.health >= 9 * hero.maxhealth)
				if (hero.level >= minlevel)
				if (hero.item_count < hero.max_items)
					bCanGetItem = true;
				if (hero.health > hero.maxhealth * 20 / 100)
					hero.Damage(hero.health - hero.maxhealth * 20 / 100);
				if(hero.CmdCount == 1)
					hero.AddCommand(true, "move", .GetExitPoint(hero, .pos)+Point(10, 50));
				hero.KillCommand();
				if (timetorest == 0 && bCanGetItem)
				{
					minlevel += .levelperitem;
					EnvWriteInt(this, "minlevel", minlevel);
					Sleep(250);
					.GiveItem(item_obj, hero);
					timetorest = 600;
				} else
				{
					continue;
				}
			}
		}
	}
	if(timetorest == 1 && !bRespawn)
	{
		bRespawn = true;
	}		
	if(timetorest > 0)
		timetorest -= 1;

	Sleep(1000);
	if(MapSize < 32000){
		olBarbarian = Union(ClassPlayerObjs("RebelLiberatus", 15), ClassPlayerObjs("RebelJavelinThrower", 15)).GetObjList();
		//pr(olBarbarian.count);
		olBarbarian.ClearDead();	
		if(olBarbarian.count < 120){
			timeup = (10+rand(3))*60;
			brespawn += 1;
			if(brespawn >= timeup){
				EnvWriteInt("AttackTo", 20);
				if(EnvReadInt("StopBarbarianItem") != 1)
					EnvWriteInt("StopBarbarianItem", 1);
		 
				brespawn = 0;
				randb = 10+rand(12);
				for (i=0; i<randb; i+=1){
					if(rand(2) == 0) { class_unit = "RebelLiberatus"; }
					else { class_unit = "RebelJavelinThrower"; }
					
					if(!.settlement.IsFull){
						uBarbarian = Place(class_unit, .pos, 15);
						if(uBarbarian.IsValid){
							.settlement.ForceAddUnit(uBarbarian);
							
							rc = GetMapRect();
							pos.Set(0, .sight / 2);
							pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); uBarbarian.SetCommand("advance", p);
							pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); uBarbarian.AddCommand(false, "patrol", p);
							pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); uBarbarian.AddCommand(false, "patrol", p);
							pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); uBarbarian.AddCommand(false, "patrol", p);
							pos.Rot(rand(360));     p = pos + .pos; p.IntoRect(rc); uBarbarian.AddCommand(false, "patrol", p);
						}
					}
					Sleep(3000);
					//new_sentry = Place(class_unit, .pos+Point(-170, 80), .player);
				}
			}
		}
	}
}
